﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication7
{
    public partial class About : Page
    {
        public DataTable da = new DataTable();
        public static readonly string sql = "select *from Articles";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) { DataFill(); }
            

        }
        //刷新
        public void DataFill()
        {
            da = DbHelper.GetData(sql);
            GridView1.DataSource = da;
            GridView1.DataBind();
        }

        //编辑
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            DataFill();
        }
        //取消
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            DataFill();

        }
        //删除
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var Id = GetDAT(e.RowIndex, 0);
           var sql = string.Format("delete Articles where Id='{0}'",Id);
            DbHelper.AddOrUpdateOrDe(sql);
            DataFill();
            GridView1.EditIndex = -1;
        }//Title Content  Author Remarks Id Articles
        //添加和更改
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var Id = GetDAT(e.RowIndex, 0);
            var Title = GetDAT(e.RowIndex, 1);
            var Content = GetDAT(e.RowIndex, 2);
            var Author = GetDAT(e.RowIndex, 3);
            var Remarks = GetDAT(e.RowIndex, 4);
            var ISOK = int.TryParse(Id, out int i);
            if (ISOK) {
                //var sql = string.Format("",); ='{}', 
                var sql = string.Format("update Articles set Title='{0}', Content='{1}',  Author='{2}', Remarks='{3}' where id={4}", Title, Content , Author ,Remarks ,Id);
                DbHelper.AddOrUpdateOrDe(sql);
                GridView1.EditIndex = -1;
                DataFill();
                
            } 
            else 
                    {
                var sql = string.Format("insert into Articles (Title ,Content,  Author, Remarks) values('{0}','{1}','{2}','{3}')", Title, Content, Author, Remarks);
                DbHelper.AddOrUpdateOrDe(sql);
                GridView1.EditIndex = -1;
                DataFill();
               
            }
        }
        //放回位置
        public  string GetDAT(int i,int c) 
        {
            var id = GridView1.Rows[i].Cells[c];
            var res = id.Controls.Count > 0 ? ((TextBox)id.Controls[0]).Text.ToString() : id.Text;
            return res;
                }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        //添加行
        protected void Button1_Click(object sender, EventArgs e)
        {
            string sql = "select *from Articles";
            da= DbHelper.GetData(sql);
           var dt=da.Copy();
            dt.Rows.Add(dt.NewRow());
            GridView1.EditIndex = da.Rows.Count;
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}